alert('SceneSetting.js loaded');



//biến âm thanh
var indexhinh1=0, indexhinh2=0 , imdex =0, cot=0;

//lay du lieu file setting
function LayDuLieuSetting(){
	if  (window.XMLHttpRequest)
	  {
	  xhttp=new  XMLHttpRequest();
	  }
	  else
	  {
	  xhttp=new  ActiveXObject("Microsoft.XMLHTTP");
	  }
	  xhttp.open("GET","app/data/datasetting.xml",false);
	
		xhttp.send();
		xmlDoc=xhttp.responseXML;
		x=xmlDoc.getElementsByTagName("a");
	
		
		indexhinh1 = x[0].getElementsByTagName("b")[0].childNodes[0].nodeValue;
		indexhinh2 = x[0].getElementsByTagName("c")[0].childNodes[0].nodeValue;
			alert("lay du lieusssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss setting: " + indexhinh1+" va "+ indexhinh2 );
			initiFocus();
}



//khởi gán focus
function initiFocus(){
	switch (indexhinh1) {
		case 0:
			$('#checkmenuhinh11').sfCheckBox('check');
			break;
		case 1:
			$('#checkmenuhinh12').sfCheckBox('check');
			break;
		case 2:
			$('#checkmenuhinh13').sfCheckBox('check');
			break;
	}
	switch (indexhinh2) {
		case 0:
			$('#checkmenuhinh21').sfCheckBox('check');
			break;
		case 1:$('#checkmenuhinh22').sfCheckBox('check');
			break;
		case 2:$('#checkmenuhinh23').sfCheckBox('check');
			break;
	}
	alert("lay du lieu ne setting: " + indexhinh1+" va "+ indexhinh2 );
}



function Focus(){
	switch (cot) {
	case 0:
		if(imdex==0){
			document.images['setimg1'].src="images/Caidat/hinh anh - Copy.png";
			document.images['setimg2'].src="images/Caidat/amthanh.png";
			document.images['setimg6'].src="images/Caidat/thoat.png";
			
			document.getElementById("sethinhanh").className="settingHienthi";
			document.getElementById("setamthanh").className="settingBienmat";
			document.getElementById("setanhsang").className="settingBienmat";
			document.getElementById("setngonngu").className="settingBienmat";
			
		}else if (imdex==1) {
			document.images['setimg2'].src="images/Caidat/amthanh - Copy.png";
			document.images['setimg1'].src="images/Caidat/hinh anh.png";
			document.images['setimg3'].src="images/Caidat/anhsang.png";

			document.getElementById("setamthanh").className="settingHienthi";
			document.getElementById("sethinhanh").className="settingBienmat";
			document.getElementById("setanhsang").className="settingBienmat";

			
		}
		else if (imdex==2) {
			document.images['setimg3'].src="images/Caidat/anhsang - Copy.png";
			document.images['setimg2'].src="images/Caidat/amthanh.png";
			document.images['setimg4'].src="images/Caidat/Lang.png";
			
			document.getElementById("setanhsang").className="settingHienthi";
			
			document.getElementById("setamthanh").className="settingBienmat";
			document.getElementById("setngonngu").className="settingBienmat";
		}
		else if (imdex==3) {
			document.images['setimg4'].src="images/Caidat/Lang - Copy.png";
			document.images['setimg3'].src="images/Caidat/anhsang.png";
			document.images['setimg5'].src="images/Caidat/save.png";

			document.getElementById("setngonngu").className="settingHienthi";
			document.getElementById("setanhsang").className="settingBienmat";
			document.getElementById("setsave").className="settingBienmat";
		}
		else if (imdex==4) {
			document.images['setimg5'].src="images/Caidat/save - Copy.png";
			document.images['setimg4'].src="images/Caidat/Lang.png";
			document.images['setimg6'].src="images/Caidat/thoat.png";

			document.getElementById("setngonngu").className="settingBienmat";/////////////////////////
		}
		else if (imdex==5) {
			document.images['setimg6'].src="images/Caidat/thoat - Copy.png";
			document.images['setimg1'].src="images/Caidat/hinh anh.png";
			document.images['setimg5'].src="images/Caidat/save.png";
			document.getElementById("setimg6").className="itemfocus";
		}
		break;
	case 1:
		if(indexhinh1==0){
			$('#checkmenuhinh11').sfCheckBox('check');
			$('#checkmenuhinh12').sfCheckBox('uncheck');
			$('#checkmenuhinh13').sfCheckBox('uncheck');
			document.getElementById("bg11").className="itemfocus";
			document.getElementById("bg12").className="itemblur";
			document.getElementById("bg13").className="itemblur";
			
			document.getElementById("bg23").className="itemblur";
			document.getElementById("bg22").className="itemblur";
			document.getElementById("bg21").className="itemblur";
			
		}
		else if (indexhinh1==1) {
			$('#checkmenuhinh12').sfCheckBox('check');$('#checkmenuhinh11').sfCheckBox('check');
			$('#checkmenuhinh11').sfCheckBox('uncheck');
			$('#checkmenuhinh13').sfCheckBox('uncheck');
			document.getElementById("bg12").className="itemfocus";
			document.getElementById("bg11").className="itemblur";
			document.getElementById("bg13").className="itemblur";
			
			document.getElementById("bg23").className="itemblur";
			document.getElementById("bg22").className="itemblur";
			document.getElementById("bg21").className="itemblur";
		}
		else if (indexhinh1==2) {
			$('#checkmenuhinh13').sfCheckBox('check');
			$('#checkmenuhinh11').sfCheckBox('uncheck');
			$('#checkmenuhinh12').sfCheckBox('uncheck');
			document.getElementById("bg13").className="itemfocus";
			document.getElementById("bg12").className="itemblur";
			document.getElementById("bg11").className="itemblur";
			
			document.getElementById("bg23").className="itemblur";
			document.getElementById("bg22").className="itemblur";
			document.getElementById("bg21").className="itemblur";
		}
			
		break;
	case 2:
		if(indexhinh2==0){
			$('#checkmenuhinh21').sfCheckBox('check');
			$('#checkmenuhinh22').sfCheckBox('uncheck');
			$('#checkmenuhinh23').sfCheckBox('uncheck');
			document.getElementById("bg21").className="itemfocus";
			document.getElementById("bg22").className="itemblur";
			document.getElementById("bg23").className="itemblur";
			
			document.getElementById("bg13").className="itemblur";
			document.getElementById("bg12").className="itemblur";
			document.getElementById("bg11").className="itemblur";
		}
		else if (indexhinh2==1) {
			$('#checkmenuhinh22').sfCheckBox('check');
			$('#checkmenuhinh21').sfCheckBox('uncheck');
			$('#checkmenuhinh23').sfCheckBox('uncheck');
			document.getElementById("bg22").className="itemfocus";
			document.getElementById("bg21").className="itemblur";
			document.getElementById("bg23").className="itemblur";
			
			document.getElementById("bg13").className="itemblur";
			document.getElementById("bg12").className="itemblur";
			document.getElementById("bg11").className="itemblur";
		}
		
		else if (indexhinh2==2) {
			$('#checkmenuhinh23').sfCheckBox('check');
			$('#checkmenuhinh21').sfCheckBox('uncheck');
			$('#checkmenuhinh22').sfCheckBox('uncheck');
			document.getElementById("bg23").className="itemfocus";
			document.getElementById("bg22").className="itemblur";
			document.getElementById("bg21").className="itemblur";
			
			document.getElementById("bg13").className="itemblur";
			document.getElementById("bg12").className="itemblur";
			document.getElementById("bg11").className="itemblur";
		}
		break;
	}
}

function SetBg(){
	alert("Indexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx "+indexhinh1);
	
	if (indexhinh1==0)
	document.getElementById("SceneMain").style.background="url(images/menu/bg.jpg)";
	else if (indexhinh1==1)
		document.getElementById("SceneMain").style.background="url(images/menu/bg122.jpg)";
	else if (indexhinh1==2)
		document.getElementById("SceneMain").style.background="url(images/menu/bg113.jpg)";
	
	if (indexhinh2==0)
		document.getElementById("SceneMore").style.background="url(images/Morong/bg.jpg)";
	else if (indexhinh2==1)
		document.getElementById("SceneMore").style.background="url(images/menu/bg112.jpg)";
	else if (indexhinh2==2)
		document.getElementById("SceneMore").style.background="url(images/menu/bg223.jpg)";
	
	
}

function handsettingclick(){
	document.getElementById("setimg1").addEventListener('click', function() {
		document.images['setimg1'].src="images/Caidat/hinh anh - Copy.png";
		document.images['setimg2'].src="images/Caidat/amthanh.png";
		document.images['setimg6'].src="images/Caidat/thoat.png";
		document.images['setimg4'].src="images/Caidat/Lang.png";
		document.images['setimg3'].src="images/Caidat/anhsang.png";
		document.images['setimg5'].src="images/Caidat/save.png";
		
		document.getElementById("sethinhanh").className="settingHienthi";
		document.getElementById("setamthanh").className="settingBienmat";
		document.getElementById("setanhsang").className="settingBienmat";
		document.getElementById("setngonngu").className="settingBienmat";
		
		imdex=0;
	});
	document.getElementById("setimg2").addEventListener('click', function() {
		document.images['setimg2'].src="images/Caidat/amthanh - Copy.png";
		document.images['setimg1'].src="images/Caidat/hinh anh.png";
		document.images['setimg3'].src="images/Caidat/anhsang.png";
		document.images['setimg6'].src="images/Caidat/thoat.png";
		document.images['setimg4'].src="images/Caidat/Lang.png";
		document.images['setimg5'].src="images/Caidat/save.png";

		document.getElementById("setamthanh").className="settingHienthi";
		document.getElementById("sethinhanh").className="settingBienmat";
		document.getElementById("setanhsang").className="settingBienmat";
		document.getElementById("setngonngu").className="settingBienmat";
		
		imdex=1;
	});
	document.getElementById("setimg3").addEventListener('click', function() {
		document.images['setimg3'].src="images/Caidat/anhsang - Copy.png";
		document.images['setimg2'].src="images/Caidat/amthanh.png";
		document.images['setimg4'].src="images/Caidat/Lang.png";
		document.images['setimg6'].src="images/Caidat/thoat.png";
		document.images['setimg5'].src="images/Caidat/save.png";
		document.images['setimg1'].src="images/Caidat/hinh anh.png";
		
		document.getElementById("setanhsang").className="settingHienthi";
		
		document.getElementById("setamthanh").className="settingBienmat";
		document.getElementById("setngonngu").className="settingBienmat";
		document.getElementById("sethinhanh").className="settingBienmat";
		
		imdex=2;
	});
	document.getElementById("setimg4").addEventListener('click', function() {
		document.images['setimg4'].src="images/Caidat/Lang - Copy.png";
		document.images['setimg3'].src="images/Caidat/anhsang.png";
		document.images['setimg5'].src="images/Caidat/save.png";
		document.images['setimg2'].src="images/Caidat/amthanh.png";
		document.images['setimg6'].src="images/Caidat/thoat.png";
		document.images['setimg1'].src="images/Caidat/hinh anh.png";

		document.getElementById("setngonngu").className="settingHienthi";
		document.getElementById("setanhsang").className="settingBienmat";
		document.getElementById("setsave").className="settingBienmat";
		document.getElementById("setamthanh").className="settingBienmat";
		document.getElementById("sethinhanh").className="settingBienmat";
		
		imdex=3;
	});
	document.getElementById("setimg5").addEventListener('click', function() {
		document.images['setimg5'].src="images/Caidat/save - Copy.png";
		document.images['setimg4'].src="images/Caidat/Lang.png";
		document.images['setimg6'].src="images/Caidat/thoat.png";
		document.images['setimg2'].src="images/Caidat/amthanh.png";
		document.images['setimg1'].src="images/Caidat/hinh anh.png";
		document.images['setimg3'].src="images/Caidat/anhsang.png";

		document.getElementById("setngonngu").className="settingBienmat";
		document.getElementById("setanhsang").className="settingBienmat";
		document.getElementById("setamthanh").className="settingBienmat";
		document.getElementById("sethinhanh").className="settingBienmat";
		
		imdex=4;
		$('#PopupSave').sfPopup('show');
	
		
	});
	document.getElementById("setimg6").addEventListener('click', function() {
		document.images['setimg6'].src="images/Caidat/thoat - Copy.png";
		document.images['setimg1'].src="images/Caidat/hinh anh.png";
		document.images['setimg5'].src="images/Caidat/save.png";
		document.images['setimg4'].src="images/Caidat/Lang.png";
		document.images['setimg2'].src="images/Caidat/amthanh.png";
		document.images['setimg3'].src="images/Caidat/anhsang.png";
		document.getElementById("setimg6").className="itemfocus";
		
		imdex=5;
		$('#PopupThoat').sfPopup('show');
	});
}

function SceneSetting() {

	this.initialize = function () {
		alert("SceneSetting.initialize()");
		handsettingclick();
		//gan cac item focus
		initiFocus();
		
		//
		Focus();
		$('#checkmenuhinh11').sfCheckBox({});
		$('#checkmenuhinh12').sfCheckBox({});
		$('#checkmenuhinh13').sfCheckBox({});
		$('#checkmenuhinh21').sfCheckBox({});
		$('#checkmenuhinh22').sfCheckBox({});
		$('#checkmenuhinh23').sfCheckBox({});
		$('#svecSliderbaramthanh').sfSlider({
			max:100,
			value:40
		});
		
		$('#svecSliderbartiengdong').sfSlider({
			max:100,
			value: 50
		});
		$('#svecSliderbarAnhsang').sfSlider({
			max:100,
			value:40
		});
		$('#PopupSave').sfPopup({
			text:'Bạn có muốn lưu không?',
			buttons:['Ok', 'Cancel'],
			defaultFocus: 1, 
			callback:function(selectedIndex){
				alert('selected button index: ' + selectedIndex);
				if(selectedIndex==0){
					sf.scene.hide('Setting');
					sf.scene.show('Main');
					sf.scene.focus('Main');
					SetBg();
				}
			}
		});
		$('#PopupThoat').sfPopup({
			text:'Thoát không lưu?',
			defaultFocus: 1, 
			buttons:['Ok', 'Cancel'],
			callback:function(selectedIndex){
				alert('selected button index: ' + selectedIndex);
				if(selectedIndex==0){
					sf.scene.hide('Setting');
					sf.scene.show('Main');
					sf.scene.focus('Main');
					
				}
			}
		});
		$('#listnhacnen').sfList({
			data:['Nhạc nền 1', 'Nhạc nền 2', 'Nhạc nền 3'],
			index:0,itemsPerPage: 1
		});
		$('#checkMuteNhacnen').sfCheckBox({
			
		});
		$('#checkMuteAmthanh').sfCheckBox({
			
		});
		

		
		
	};

	this.handleShow = function (data) {
		alert("SceneSetting.handleShow()");
		// this function will be called when the scene manager show this scene
		$("#keyhelp").sfKeyHelp({
	        "MOVE" : "Di chuyển",
	        "ENTER" : "Chọn",
	        "RETURN" : "Trở về"
	    });
		//lay du lieu file setting
		LayDuLieuSetting();
		var helpBarInfo = {
	            helpbarType: "HELPBAR_TYPE_GESTURE_CUSTOMIZE",
	            itemList: [
	                { itemType: "HELPBAR_GESTURE_ITEM_RETURN", itemText: "Go back" },
	                { itemType: "HELPBAR_GESTURE_ITEM_POINTING_GESTURE", itemText: "Choose picture" }
	            ]
	        };

	        webapis.recognition.SetGestureHelpbarInfo(JSON.stringify(helpBarInfo));
	        
	        var helpBarInfo1 = {
	                helpbarType: "HELPBAR_TYPE_VOICE_CUSTOMIZE",
	                helpbarItemsList: [
	                    
	                    { itemText: "Left", commandList: [{command: "Left"}]},
	                    { itemText: "Right", commandList: [{command: "Right"}]},
	                    { itemText: "Up", commandList: [{command: "Up"}]},
	                    { itemText: "Down", commandList: [{command: "Down"}]},
	                    { itemText: "Open", commandList: [{command: "Open"}]},
	                    { itemText: "Close", commandList: [{command: "Close"}]},
	                    { itemText: "Exit", commandList: [{command: "Exit"}]}
	                ]
	            };

	            webapis.recognition.SetVoiceHelpbarInfo(JSON.stringify(helpBarInfo1));
	};

	this.handleHide = function () {
		alert("SceneSetting.handleHide()");
		// this function will be called when the scene manager hide this scene
	};

	this.handleFocus = function () {
		alert("SceneSetting.handleFocus()");
		document.getElementById("keyhelp").style.visibility = "visible";
		GestureDispatcher.currentScene = this;
        VoiceDispatcher.currentScene = this;
        Grid.hienthiketnoi();
	};

	this.handleBlur = function () {
		alert("SceneSetting.handleBlur()");
		// this function will be called when the scene manager move focus to another scene from this scene
	};
	
	this.handleKeyDown = function (keyCode) {
		alert("SceneSetting.handleKeyDown(" + keyCode + ")");
		// TODO : write an key event handler when this scene get focued
		switch (keyCode) {
			case sf.key.LEFT:
				if(cot>0){
					cot--;
					Focus();
				}
				alert("set cot=" + cot + "indexhinh1="+indexhinh1+"imdex"+imdex);
				break;
			case sf.key.RIGHT: 
				if(cot<2){
					cot++;
					Focus();
				}alert("set cot=" + cot + "indexhinh1="+indexhinh1+"imdex"+imdex);
				break;
			case sf.key.UP:
				if(imdex>0 && cot==0){
					imdex--;
					Focus();
				}
				else if(indexhinh1>0 && cot==1){
					indexhinh1--;
					Focus();
				}
				else if(indexhinh2>0 && cot==2){
					indexhinh2--;
					Focus();
				}
				alert("set cot=" + cot + "indexhinh1="+indexhinh1+"imdex"+imdex);
				break;
			case sf.key.DOWN:
				if(imdex<6 && cot==0){
					imdex++;
					Focus();
					alert("set cot=" + cot + "indexhinh1="+indexhinh1+"imdex"+imdex);
				}
				else if(indexhinh1<3 && cot==1){
					indexhinh1++;
					Focus();
				}
				else if(indexhinh2<3 && cot==2){
					indexhinh2++;
					Focus();
				}
				
				break;
			case sf.key.ENTER:
				
				initiFocus();
				if(cot==0){
					if(imdex==4){
						$('#PopupSave').sfPopup('show');
					}
					else if (imdex==5) {
						$('#PopupThoat').sfPopup('show');
					}
				}
				///////////////////
				
				
				
				
				
				
				////////////////////////	
				break;
			case sf.key.RETURN:
				
				
				sf.scene.hide('Setting');
				sf.scene.show('Main');
				sf.scene.focus('Main');
				
				
				
				break;
			case sf.key.EXIT:
				
				$('#PopupThoat').sfPopup('show');
				
				break;
			default:
				alert("handle default key event, key code(" + keyCode + ")");
				break;
		}
	};
	
	this.handleGesture = function(e) {
        log("SceneCookingGuide.handleGesture, type: " + e.eventtype);

        // as for now no gesture events are handled here
        // please look at ScenePhotoView.handleGesture() method
    };
    this.handleVoice = function(e) {
        log("SceneCookingGuide.handleVoicee, type: " + e.eventtype);

        switch (e.result.toLowerCase()) {
            
            case "left": 
            	if(cot>0){
					cot--;
					Focus();
				}
				alert("set cot=" + cot + "indexhinh1="+indexhinh1+"imdex"+imdex);
            	break;
            case "right": 
            	if(cot<2){
					cot++;
					Focus();
				}alert("set cot=" + cot + "indexhinh1="+indexhinh1+"imdex"+imdex);
            	break;
            case "up": 
            	if(imdex>0 && cot==0){
					imdex--;
					Focus();
				}
				else if(indexhinh1>0 && cot==1){
					indexhinh1--;
					Focus();
				}
				else if(indexhinh2>0 && cot==2){
					indexhinh2--;
					Focus();
				}
				alert("set cot=" + cot + "indexhinh1="+indexhinh1+"imdex"+imdex);
            	break;
            case "down": 
            	if(imdex<6 && cot==0){
					imdex++;
					Focus();
					alert("set cot=" + cot + "indexhinh1="+indexhinh1+"imdex"+imdex);
				}
				else if(indexhinh1<3 && cot==1){
					indexhinh1++;
					Focus();
				}
				else if(indexhinh2<3 && cot==2){
					indexhinh2++;
					Focus();
				}
            	break;
            case "open": 
            	initiFocus();
				if(cot==0){
					if(imdex==4){
						$('#PopupSave').sfPopup('show');
					}
					else if (imdex==5) {
						$('#PopupThoat').sfPopup('show');
					}
				}
            	break;
            case "close":
                sf.scene.hide("Setting");
                sf.scene.show("Main");
                sf.scene.focus("Main");
                sf.key.preventDefault();
                break;
            case "exit": $('#PopupThoat').sfPopup('show'); break;
        }
    };
};



